<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" icon="el-icon-circle-plus-outline" @click="showDialogVisible(null)">新增</el-button>
    </div>

    <el-table
      :data="tableInfo.data"
      border
      style="width:100%"
      class="init_table">

      <el-table-column
        align="center"
        width="50"
        type="index"
        label="序号">
      </el-table-column>

      <el-table-column
        align="center"
        prop="name"
        label="单位名称"
        :min-width="tableInfo.minWidth">
      </el-table-column>

      <el-table-column
        align="center"
        prop="creditCode"
        label="信用证代码"
        :min-width="tableInfo.minWidth">
      </el-table-column>
      <el-table-column
        align="center"
        prop="liaison"
        label="联系人"
        :min-width="tableInfo.minWidth">
      </el-table-column>

      <el-table-column
        align="center"
        prop="phoneNumber"
        label="联系电话"
        :min-width="tableInfo.minWidth">
      </el-table-column>

      <el-table-column
        align="center"
        width="200"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="showDialogVisible(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination v-show="total>0"
                     :current-page="tableQuery.pageNum"
                     :page-sizes="[10,20,30, 50]"
                     :page-size="tableQuery.pageSize"
                     :total="total"
                     background layout="total, sizes, prev, pager, next, jumper"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"/>
    </div>


    <el-dialog title="电梯" :visible.sync="dialogVisible" width="900px" top="10vh">
      <el-form ref="submitForm" :rules="rules" :model="submitData" label-position="right" label-width="130px">
        <el-row>
          <el-col span="11">
            <el-form-item label="设备注册代码：" prop="registrationCode">
              <el-input v-model="submitData.registrationCode"/>
            </el-form-item>
          </el-col>
          <el-col span="11" offset="1">
            <el-form-item label="电梯型号：" prop="elevatorNorms">
              <el-input v-model="submitData.elevatorNorms"/>

            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="11">
            <el-form-item label="电梯类型：" prop="elevatorType">
              <el-select v-model="submitData.elevatorType" clearable placeholder="请选择" style="width:100%">
                <el-option
                  v-for="item in enumData.elevatorType"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key">
                </el-option>
              </el-select>

            </el-form-item>
          </el-col>
          <el-col span="11" offset="1">
            <el-form-item label="产品出厂编号：" prop="manufactureNumber">
              <el-input v-model="submitData.manufactureNumber"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="11">
            <el-form-item label="产品出厂日期：" prop="manufactureTime">
              <el-date-picker
                v-model="submitData.manufactureTime"
                type="date"
                placeholder="选择日期"
                style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col span="11" offset="1">
            <el-form-item label="位置码：" prop="addressCode">
              <el-input v-model="submitData.addressCode"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col span="11">
            <el-form-item label="国产或进口：" prop="madeInChain">

              <el-select v-model="submitData.madeInChain" clearable placeholder="请选择" style="width:100%">
                <el-option
                  v-for="item in [{key:1,value:'国产'},{key:2,value:'进口'}]"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key">
                </el-option>
              </el-select>

            </el-form-item>
          </el-col>
          <el-col span="11" offset="1">
            <el-form-item label="载重量(KG)：" prop="deadWeight">
              <el-input type="number"	 v-model="submitData.deadWeight"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col span="11">
            <el-form-item label="额定速度(M/S)：" prop="speed">
              <el-input v-model="submitData.speed"/>
            </el-form-item>
          </el-col>
          <el-col span="11" offset="1">
            <el-form-item label="层、站、门：" prop="floorNumber">
              <el-col span="6">
                <el-form-item prop="date1">
                  <el-input type="number"	 v-model="submitData.floorNumber" placeholder="层"/>
                </el-form-item>
              </el-col>
              <el-col span="6" offset="3">
                <el-form-item prop="date1">
                  <el-input type="number" v-model="submitData.stationNumber" placeholder="站"/>
                </el-form-item>
              </el-col>
              <el-col span="6" offset="3">
                <el-form-item prop="date1">
                  <el-input type="number" v-model="submitData.doorNumber" placeholder="门"/>
                </el-form-item>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="11">
            <el-form-item label="年检日期：" prop="annualInspectionTime">
              <el-date-picker
                v-model="submitData.annualInspectionTime"
                type="date"
                placeholder="选择日期"
                style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col span="11" offset="1">
            <el-form-item label="限速器校验日期：" prop="speedLimiterCheckTime">
              <el-date-picker
                v-model="submitData.speedLimiterCheckTime"
                type="date"
                placeholder="选择日期"
                style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col span="11">
            <el-form-item label="制动试验日期：" prop="brakeCheckTime">
              <el-date-picker
                v-model="submitData.brakeCheckTime"
                type="date"
                placeholder="选择日期"
                style="width:100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col span="11" offset="1">
            <el-form-item label="经纬度：">
              <el-col span="11">
                <el-form-item prop="date1">
                  <el-input v-model="submitData.longitude" placeholder="经"/>
                </el-form-item>
              </el-col>
              <el-col span="11" offset="2">
                <el-form-item prop="date2">
                  <el-input v-model="submitData.latitude" placeholder="纬"/>
                </el-form-item>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button v-if="operateType=='add'" type="primary" @click="handleAddData">确定</el-button>
        <el-button v-else type="primary" @click="handleModifyData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import ele from '@/api/ele'
  import {handleRequest,enumData} from '@/utils/system'

  export default {
    data: () => ({
      dialogVisible: false,
      operateType: null,
      enumData:{
        elevatorType: null
      },
      submitData: {
        id: null,
        registrationCode: null,
        elevatorNorms: null,
        elevatorType: null,
        manufactureNumber: null,
        manufactureTime: null,
        addressCode: null,
        madeInChain: null,
        deadWeight: null,
        speed: null,
        floorNumber: null,
        stationNumber: null,
        doorNumber: null,
        annualInspectionTime: null,
        speedLimiterCheckTime: null,
        brakeCheckTime: null,
        longitude: null,
        latitude: null
      },
      rules: {
        name: [{required: true, message: '不能为空', trigger: 'change'}],
        creditCode: [{required: true, message: '不能为空', trigger: 'change'}],
        phoneNumber: [{required: true, message: '不能为空', trigger: 'change'}],
        liaison: [{required: true, message: '不能为空', trigger: 'change'}]
      },
      tableQuery: {
        creditCode: null,
        name: null,
        pageNum: 1,
        pageSize: 10,
        sort: 'create_date',
        order: 'descending'
      },
      tableInfo: {
        data: null,
        total: null,
        minWidth: 50
      }

    }),
    mounted() {
      this.enumData = enumData();
      this.refreshTable();
    },
    methods: {
      resetSubmitData() {
        this.submitData = {
          id: null,
          name: null,
          creditCode: null,
          liaison: null,
          phoneNumber: null
        }
      },
      showDialogVisible(row) {
        this.resetSubmitData()
        this.operateType = 'add'
        this.dialogVisible = true
        if (row) {
          this.submitData = {...row}
          this.operateType = 'modify'
        }
        this.$nextTick(() => {
          this.$refs['submitForm'].clearValidate()
        })
      },
      handleAddData() {
        this.$refs['submitForm'].validate((valid) => {
          if (valid) {
            handleRequest({
              method: ele.elevator.add,
              data: this.submitData,
              operateType: "新增",
              warnAlertShow: true,
              successAlertShow: true,
              failAlertShow: true,
              successCall: () => {
                this.dialogVisible = false;
                this.refreshTable();
              }
            })
          }
        })
      },
      handleModifyData() {
        this.$refs['submitForm'].validate((valid) => {
          if (valid) {
            handleRequest({
              method: ele.elevator.modify,
              data: this.submitData,
              operateType: "修改",
              warnAlertShow: true,
              successAlertShow: true,
              failAlertShow: true,
              successCall: () => {
                this.dialogVisible = false;
                this.refreshTable();
              }
            })
          }
        })
      },
      refreshTable() {
        handleRequest({
          method: ele.elevator.list,
          data: this.tableQuery,
          successCall: (data) => {
            this.tableInfo.data = data.rows
            this.tableInfo.total = data.total
          }
        })
      },
      handleSizeChange(val) {
        this.tableQuery.pageSize = val
        this.refreshTable()
      },
      handleCurrentChange(val) {
        this.tableQuery.pageNum = val
        this.refreshTable()
      },
    }
  }
</script>
